package com.firesrc.web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;

import com.firesrc.bean.CountBean;

/**
 * Servlet implementation class ViewKeywordByDate
 */
public class ViewKeywordByDate extends SuperServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewKeywordByDate() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("GBK");
		response.setContentType("text/html; charset=gb2312");
		new DbThreadSession(cp);
		DbOomQuery q = new DbOomQuery(
				"select createDate as name ,count(id) as count from keyword groupbycount group by createDate");
		ResultSet execute = q.execute();
		StringBuffer buffer = new StringBuffer();
		CountBean countBean = new CountBean();
		List<CountBean> countBeanList = new ArrayList<CountBean>();
		try {
			while (execute.next()) {
				countBean = new CountBean();
				String createDate = execute.getString(1);
				if (createDate != null && createDate.equals("null")) {
					createDate = "''";
				}
				if (createDate != null) {
					createDate = createDate.substring(0, 10);
				}
				countBean.setName(createDate);
				countBean.setCount(execute.getInt(2));
				countBeanList.add(countBean);
				//				buffer.append("<a href='/ViewAllKeyword?date=" + createDate + "' a>" + createDate + "("
				//						+ execute.getInt(2) + ") </a>  &nbsp; &nbsp; ");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("list", countBeanList);
		request.getRequestDispatcher("datekeywordcount.jsp").forward(request, response);
		response.getWriter().println(buffer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		doGet(request, response);
	}

}
